1
/****************************** Module Header ******************************\
2 * Module Name: Global.asax.cs
3 * Project: CSASPNETBackgroundWorker
4 * Copyright (c) Microsoft Corporation
6 * When application starts up, Application_Start() method will be called.
7 * In the Application_Start() method, it creates a BackgroundWorker object and
8 * then stores it in Application State. Therefore, the worker_DoWork() will
9 * keep executing until application ends.
11 * This source is subject to the Microsoft Public License.
12 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
13 * All other rights reserved.
15 \*****************************************************************************/
18 using System
.Threading
;
20 namespace CSASPNETBackgroundWorker
22 public class Global
: System
.Web
.HttpApplication
25 /// Create a Background Worker to run the operation
26 /// whenever the application start.
28 protected void Application_Start(object sender
, EventArgs e
)
30 BackgroundWorker worker
= new BackgroundWorker();
31 worker
.DoWork
+= new BackgroundWorker
.DoWorkEventHandler(worker_DoWork
);
32 worker
.RunWorker(null);
34 // This Background Worker is Applicatoin Level,
35 // so it will keep working and it is shared by all users.
36 Application
["worker"] = worker
;
40 /// This operation will work without the end.
42 void worker_DoWork(ref int progress
,
43 ref object _result
, params object[] arguments
)
45 // Do the operation every 1 second wihout the end.
50 // This statement will run every 1 second.
53 // Other logic which you want it to keep running.
54 // You can do some scheduled tasks here by checking DateTime.Now.